<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--企业数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-containers">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--小区数据-->
      <el-col :span="20" :xs="24">
        <el-card class="box-card">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="阀门名称" prop="valveName">
              <el-input v-model="queryParams.valveName" placeholder="请输入阀门名称" clearable style="width: 150px" />
            </el-form-item>
            <!--              <el-form-item label="到期时间" prop="expiresTime">-->
            <!--                <el-date-picker v-model="queryParams.expiresTime" type="date" placeholder="选择日期"-->
            <!--                        value-format="yyyy-MM-dd HH:mm:ss" >-->
            <!--                    </el-date-picker>-->
            <!--              </el-form-item>-->
            <el-form-item label="到期时间">
              <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                v-hasPermi="['system:valve:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                v-hasPermi="['system:valve:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="userList" border>
            <!-- <el-table-column type="selection" width="50" align="center" /> -->
            <el-table-column label="阀门名称" align="center" key="valveName" prop="valveName" :show-overflow-tooltip="true" />
            <el-table-column label="阀门类型" align="center" key="valveType" prop="valveType" :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="安装时间" align="center" key="installTime" prop="installTime"
              :show-overflow-tooltip="true" />
            <el-table-column label="到期时间" align="center" key="expiresTime" prop="expiresTime"
              :show-overflow-tooltip="true" />
            <el-table-column label="描述" align="center" key="remark" prop="remark" :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:valve:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['system:valve:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>

      </el-col>
    </el-row>

    <!-- 添加或修改小区配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="阀门名称" prop="valveName" required>
              <el-input v-model="form.valveName" placeholder="请输入阀门名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="阀门类型" prop="valveType">
              <el-input v-model="form.valveType" placeholder="请输入阀门类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安装时间" prop="installTime">
              <el-date-picker v-model="form.installTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss"
                style="width:100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="到期时间" prop="expiresTime">
              <el-date-picker v-model="form.expiresTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss"
                style="width:100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="描述" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { areaTreeList } from "@/api/fileManagement/areainfo.js"
import { valveList, valveInfo, addValve, updateValve, delValve, exportCustome } from "@/api/operate/valve.js"
import { listSupplier, } from "@/api/system/supplierInfo.js";


export default {
  name: "civilArchives",
  components: {},
  dicts: ["customer_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      idName: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小区表格数据
      userList: null,
      // 弹出层标题
      title: "",
      //表计弹出层标题
      title2: "",
      // 企业树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 企业名称
      deptName: '',
      // 日期范围
      dateRange: [],
      //是否表计显示弹出层
      bjOPen: false,
      // 表单参数
      form: {},
      form1: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        valveName: undefined,
        expiresTime: undefined,
        valveType: undefined,
        deptId: undefined,
      },
      // 表单校验
      rules: {
        valveName: [
          { required: true, message: "阀门名称不能为空", trigger: "blur" },
        ],
        expiresTime: [
          { required: true, message: "到期时间不能为空", trigger: "blur" },
        ],
        installTime: [
          { required: true, message: "安装时间不能为空", trigger: "blur" },
        ],
        valveType: [
          { required: true, message: "阀门类型不能为空", trigger: "change" },
        ],
      },
      supplierList: [],
      schemeList: [],
      customStaus: false,
      areID: null,
      addStatue: true, //新增按钮状态
    };
  },
  watch: {
    // 根据名称筛选企业树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
    form: {
      deep: true,
      handler(val) {
        this.form = val
      },
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    // 节点单击事件
    handleNodeClick(data) {
      this.addStatue = false
      this.queryParams.areaId = data.id;
      this.handleQuery();
      this.areID = data.id
      this.getTreeList()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/valve/export",
        {
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },

    // 树查询
    treeBlur(event) {
      console.log(event)
    },
    /** 查询小区列表 */
    getList() {
      this.loading = true;
      valveList(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询企业下拉树结构 */
    getDeptTree() {
      areaTreeList().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      console.log(data)
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.areaId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加阀门";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.id;
      valveInfo(userId).then((response) => {
        this.form = response.data;
        this.$set(this.form, "postIds", response.postIds);
        this.$set(this.form, "roleIds", response.roleIds);
        this.open = true;
        this.title = "修改阀门";
        this.form.password = "";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateValve(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.areaId = this.areID
            addValve(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }

            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let userIds = ''
      let userName = ''
      if (row == '批量') {
        userIds = this.ids.join(',')
        userName = this.idName.join(',')
      } else {
        userIds = row.id
        userName = row.valveName
      }
      this.$modal
        .confirm('是否确认删阀门"' + userName + '"的数据？')
        .then(function () {
          return delValve(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
<style lang="scss" scoped>
.head-containers {
  width: 200px;
  overflow: auto;

  .el-tree {
    min-width: 100%;
    display: inline-block !important;
  }
}
</style>
  